package edu.pku.sei.transformation.editor.model.variable;

import java.util.ArrayList;

import org.w3c.dom.Element;

import edu.pku.sei.gefcommon.descriptor.annotation.EnumerationProperty;
import edu.pku.sei.gefcommon.descriptor.annotation.MethodProperty;
import edu.pku.sei.modeler.model.xml.XMLUtility;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.model.base.TypedElement;
import edu.pku.sei.transformation.util.NameValidator;

public class Variable extends TypedElement {
	// a new property to declare whether this variable should be changed by
	// transformation engine
	private boolean isFixed = false;
	/**
	 * visibility of the variable false: private true: public
	 */
	private boolean visible = false;
	public boolean isVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		if (this.visible != visible) {
			this.visible = visible;
			this.firePropertyChange(ModelPackage.VARIABLE_VISIBLE,
					!this.visible, this.visible);
		}
	}

	public Object[] getAllVisibilities() {
		return new String[] { "public", "private" };
	}

	public void setVisibility(String visibility) {
		if ("public".equals(visibility)) {
			setVisible(true);
		} else if ("private".equals(visibility)) {
			setVisible(false);
		}
	}

	@MethodProperty(id = ModelPackage.VARIABLE_VISIBLE, catalog = "Variable", name = "visibility", setter = "setVisibility")
	@EnumerationProperty(function = "getAllVisibilities")
	public String getVisibility() {
		if (visible)
			return "public";
		else
			return "private";
	}

	@MethodProperty(catalog = "Variable", id = ModelPackage.VARIBALE_FIXED, name = "isFixed", setter = "setFixed")
	public boolean isFixed() {
		return isFixed;
	}

	public void setFixed(boolean isFixed) {
		this.isFixed = isFixed;
	}

	public String getName() {
		return name;
	}

	public void setName(String identifier) {
		String oldId = this.name;
		if (!NameValidator.validate(identifier))
			return;
		if (oldId == null || !oldId.equals(identifier)) {
			this.name = identifier;
			firePropertyChange(ModelPackage.NAMEDELEMENT_NAME, oldId,
					identifier);
			if (this.container != null)
				this.container.firePropertyChange(ModelPackage.__VARIABLE__,
						oldId, identifier);
		}
	}

	@Override
	public void serialize(Element root) {
		super.serialize(root);
		XMLUtility.xmlInstance.serializeAttribute(root,
				ModelPackage.VARIBALE_FIXED, isFixed);
		XMLUtility.xmlInstance.serializeAttribute(root,
				ModelPackage.VARIABLE_VISIBLE, visible);
	}

	@Override
	public void deSerialize(Element root) {
		super.deSerialize(root);
		this.isFixed = XMLUtility.xmlInstance.deSerializeBoolean(root,
				ModelPackage.VARIBALE_FIXED);
		this.visible = XMLUtility.xmlInstance.deSerializeBoolean(root,
				ModelPackage.VARIABLE_VISIBLE);
	}

	@Override
	public String toString() {
		String prefix = null;
		if (visible)
			prefix = "+";
		else
			prefix = "-";
		return prefix + super.toString();
	}
}
